﻿using Bit.Art.Core;
using Bit.Art.Core.Extensions;
using Bit.Art.Core.Models;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using static Bit.Art.Core.Constants;

namespace Bit.Art.Api
{
    internal static class HttpApiHelper
    {
        public static Task HandleException(HttpContext context)
        {
            var feature = context.Features.Get<IExceptionHandlerFeature>();
            var exception = feature?.Error;
            if (exception == null)
                return new Task(new Action(() => { }));

            Log.Logger.Error(exception, $"异常记录 Path:{context.Request.Path} QueryString:{context.Request.QueryString}");
            context.Response.StatusCode = (int)HttpStatusCode.OK;

            if (exception is BaseCustomException)
                return context.Response.WriteAsync(new ApiCallResult(ErrorCodes.SERVER_ERROR, exception.Message).ToJson());
            else
                return context.Response.WriteAsync("hello world",Encoding.UTF8);
            //return context.Response.WriteAsync(new ApiCallResult(ErrorCodes.UNFORESEEN_ERROR, exception.Message).ToJson());
        }
    }
}
